const app = getApp()

Page({
    data: {
        CustomBar: app.globalData.CustomBar,
        theme: app.globalData.theme, //全局颜色变量
        config: app.globalData.config,
        TabCur: 0,
        MainCur: 0,
        VerticalNavTop: 0,
        goodsCategory: [],
        load: true,
        page: {
            searchCount: false,
            current: 1,
            size: 9999,
            ascs: '',//升序字段
            descs: '',
            city: ''
        },
        goodsList: [],
    },
    onLoad() {
        app.initPage()
            .then(res => {
                // 服务区域
                this.data.page.city = app.globalData.addressComponent.city
                this.goodsCategoryGet()
            })
    },
    onShow() {
        // 更新tabbar购物车数量
        // wx.setTabBarBadge({
        //   index: 2,
        //   text: app.globalData.shoppingCartCount + ''
        // })
        if(this.data.page.city != app.globalData.addressComponent.city){
            // 服务区域
            this.data.page.city = app.globalData.addressComponent.city
            this.goodsCategoryGet()
        }
    },
    goodsCategoryGet() {
        app.api.goodsCategoryGet()
            .then(res => {
                let goodsCategory = res.data || []
                if (goodsCategory.length > 0) {
                    this.setData({
                        goodsCategory: goodsCategory
                    })
                    this.goodsPage()
                }
            })
    },
    goodsPage() {
        let goodsCategory = this.data.goodsCategory || []
        app.api.goodsPage(Object.assign(
            {},
            this.data.page
        ))
            .then(res => {
                let goodsList = res.data.records || []
                if (goodsList.length > 0) {
                    goodsCategory.forEach(function (v, i) {
                        goodsList.forEach(function (t, j) {
                            if (t.categoryFirst == v.id) {
                                if (!v['goodsList']) {
                                    v['goodsList'] = []
                                }
                                v['goodsList'].push(t)
                            }
                        })
                    })
                }
                this.setData({
                    goodsCategory: goodsCategory
                })
            })
    },
    tabSelect(e) {
        this.setData({
            TabCur: e.currentTarget.dataset.id,
            MainCur: e.currentTarget.dataset.id,
            VerticalNavTop: (e.currentTarget.dataset.id - 1) * 50
        })
    },
    VerticalMain(e) {
        let that = this
        let list = this.data.goodsCategory
        let tabHeight = 0
        if (this.data.load) {
            for (let i = 0; i < list.length; i++) {
                let view = wx.createSelectorQuery().select("#main-" + i)
                view.fields({
                    size: true
                }, data => {
                    list[i].top = tabHeight
                    tabHeight = tabHeight + data.height
                    list[i].bottom = tabHeight
                }).exec()
            }
            that.setData({
                load: false,
                goodsCategory: list
            })
        }
        let scrollTop = e.detail.scrollTop + 20
        for (let i = 0; i < list.length; i++) {
            if (scrollTop > list[i].top && scrollTop < list[i].bottom) {
                that.setData({
                    VerticalNavTop: (i - 1) * 50,
                    TabCur: i
                })
                return false
            }
        }
    }
})
